package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.MenuMapper;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import com.itheima.health.vo.MenuVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();

        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(queryString)) {
            queryWrapper.like("name", queryString);
        }

        Page<Menu> page = page(new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);

        List<Menu> list = page.getRecords();
        List<MenuVO> listVo = new ArrayList<>();

        for (Menu menu : list) {
            MenuVO menuVO = new MenuVO();
            menuVO.setId(menu.getId());
            menuVO.setParentName(findParentNameByParentId(menu.getParentMenuId()));
            menuVO.setDescription(menu.getDescription());
            menuVO.setIcon(menu.getIcon());
            menuVO.setLinkUrl(menu.getLinkUrl());
            menuVO.setName(menu.getName());
            menuVO.setParentMenuId(menu.getParentMenuId());
            menuVO.setPriority(menu.getPriority());
            menuVO.setPath(menu.getPath());
            menuVO.setLevel(menu.getLevel());
            listVo.add(menuVO);
        }
        return new PageResult(page.getTotal(), listVo);
    }

    @Override
    @LcnTransaction
    public Boolean saveOrUpdateMenu(MenuVO menuVO) {
        Menu menu = new Menu();
        menu.setId(menuVO.getId());
        menu.setDescription(menuVO.getDescription());
        menu.setIcon(menuVO.getIcon());
        menu.setLinkUrl(menuVO.getLinkUrl());
        menu.setName(menuVO.getName());
        menu.setParentMenuId(getParentMenuIdByParentName(menuVO.getParentName()));
        menu.setPriority(menuVO.getPriority());

        menu.setPath(menuVO.getPath());
        menu.setLevel(menuVO.getLevel());

        return saveOrUpdate(menu);
    }

    private Integer getParentMenuIdByParentName(String parentName) {
        return  baseMapper.getParentMenuIdByParentName(parentName);
    }

    private String findParentNameByParentId(Integer parentMenuId) {
        return baseMapper.findParentNameByParentId(parentMenuId);
    }
}
